//
//  NetClient.m
//  LegoClient
//
//  Created by Balazs Faludi on 03.01.10.
//  Copyright 2010 Universität Basel. All rights reserved.
//

#import "NetClient.h"
#import "NetConnection.h"

@interface NetClient () 

@property (nonatomic) int port;
@property (nonatomic, retain) NSString *type;
@property (readwrite, retain) NSNetServiceBrowser *browser;
@property (readwrite, retain) NSMutableArray *services;
@property (readwrite, retain) NSNetService *connectedService;
@property (nonatomic, retain) NetConnection *connection;

- (void)setupConnectionToService:(NSNetService *)aService;

@end



@implementation NetClient

@synthesize delegate;
@synthesize port = _port;
@synthesize type = _type;
@synthesize browser = _browser;
@synthesize services = _services;
@synthesize isConnected;
@synthesize connectedService = _connectedService;
@synthesize connection = _connection;


#pragma mark -
#pragma mark Constructor & Desctructor

- (id)initWithType:(NSString *)type
{
	self = [super init];
	if (self != nil) {
		self.type = type;
		self.services = [[NSMutableArray alloc] initWithCapacity:3];
		self.browser = [[NSNetServiceBrowser new] autorelease];
		self.browser.delegate = self;
		isConnected = NO;
	}
	return self;
}

- (void) dealloc
{
	self.delegate = nil;
	self.type = nil;
	self.browser = nil;
	self.services = nil;
	self.connectedService = nil;
	self.connection = nil;
	[super dealloc];
}

#pragma mark -
#pragma mark Setting up connection

- (void)searchServices {
	self.services = [[NSMutableArray alloc] initWithCapacity:3];
	[self.browser searchForServicesOfType:self.type inDomain:@""];
}

- (void)connectToService:(NSNetService *)service {
	[service setDelegate:self];
	[service resolveWithTimeout:0];
}

#pragma mark -
#pragma mark NetServiceBrowserDelegate protocol

-(void)netServiceBrowser:(NSNetServiceBrowser *)aBrowser didFindService:(NSNetService *)aService moreComing:(BOOL)more {
    [self.services addObject:aService];
	if(!more && delegate){
		[delegate client:self didFindServices:self.services];
	}
}

-(void)netServiceBrowser:(NSNetServiceBrowser *)aBrowser didRemoveService:(NSNetService *)aService moreComing:(BOOL)more {
    [self.services removeObject:aService];
	isConnected = YES;
    self.connectedService = aService;
}

#pragma mark -
#pragma mark NetServiceDelegate protocol

-(void)netServiceDidResolveAddress:(NSNetService *)service {
	[self setupConnectionToService:service];
}

-(void)netService:(NSNetService *)service didNotResolve:(NSDictionary *)errorDict {
    NSLog(@"Could not resolve: %@", errorDict);
}

#pragma mark -
#pragma mark Communication

- (void)setupConnectionToService:(NSNetService *)aService {
	
	CFReadStreamRef rstr = NULL;
	//CFIndex bytes_sent = 0;
	//int send_len = 0;
	
	CFWriteStreamRef wstr = NULL;
	CFStringRef host = (CFStringRef)aService.hostName;
	UInt32 port = aService.port;
	CFStreamCreatePairWithSocketToHost(kCFAllocatorDefault, host, port, &rstr, &wstr);
	//CFWriteStreamSetProperty(wstr, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
	//CFReadStreamSetProperty(rstr, kCFStreamPropertyShouldCloseNativeSocket, kCFBooleanTrue);
	
	NSInputStream *input = (NSInputStream *)rstr;
	NSOutputStream *output = (NSOutputStream *)wstr;
	
	NetConnection *connection = [[NetConnection alloc] initWithInputStream:input outputStream:output];
	self.connection = connection;
	[connection release];
	
	if(delegate && [delegate respondsToSelector:@selector(client:connected:)]) {
		[delegate client:self connected:self.connection];
	}
	self.connectedService = aService;
	
	NSLog(@"Connected to service at address \"%@\" and port \"%i\".", aService.hostName, aService.port);
}





@end
